<?php
namespace app\shop\controller\order;


use app\common\model\car\CarPurpose;
use app\shop\controller\Controller;
use app\common\model\order\CarOrder as CarOrderModel;
use think\facade\Db;

/**
 * 普通订单
 */
class  CarOrder extends Controller
{
    public function get_month_order($status='',$reg_date = '')
    {
        $model = new CarOrderModel;
        $params = $this->postData();
        $list = $model->get_month_order($params);

        return $this->renderSuccess('', compact('list'));
    }

    //列表
    public function order_list()
    {
        $model = new CarOrderModel;
        $data = $this->request->param();
        $list = $model->get_list_admin($data);

        return $this->renderSuccess('', compact('list'));
    }

    //订单详情
    public function get_details()
    {
        $id = $this->request->param("id");
        $purpose_model = new CarPurpose();
        $purpose = $purpose_model->select()->toArray();
        $model = new CarOrderModel;
        $info = $model->admin_details($id);
        return $this->renderSuccess('', compact('info','purpose'));
    }
    public function order_post()
    {
        $data = $this->request->param();
        $model = new CarOrderModel;
        $res = $model->cancel_order($data);
        if($res){
            return $this->renderSuccess('操作成功');
        }else{
            return $this->renderError('操作失败');
        }
    }

    //删除订单
    public function del_order()
    {
        $id = $this->request->param('id');
        $model = new CarOrderModel;
        $res = $model->where(array("id"=>$id))->update(array("is_delete"=>1));
        if($res){
            return $this->renderSuccess('删除成功');
        }else{
            return $this->renderError('删除失败');
        }
    }


    //获取当日销售统计
    public function get_order_count_day()
    {
        $data = $this->request->param();

        $info = $this->get_days_order(1,$data);


        return $this->renderSuccess('', compact('info'));
    }

    //当日车主接单
    public function get_order_owner_count()
    {
        $data = $this->request->param();
        $info = $this->get_days_order(2,$data);
        return $this->renderSuccess('', compact('info'));
    }
    //当日车主退单
    public function get_order_owner_return()
    {
        $data = $this->request->param();
        $info = $this->get_days_order(3,$data);
        return $this->renderSuccess('', compact('info'));
    }

    //当日订单统计
    public function get_order_days()
    {
        $data = $this->request->param();
        $model = new CarOrderModel;
        $info = $model->get_order_day_count($data);

        return $this->renderSuccess('', compact('info'));
    }

    //月销售统计
    public function get_order_count_month()
    {
        $data = $this->request->param();

        $data['status'] = 1;
        $info = $this->get_month_order_count($data);
        return $this->renderSuccess('', compact('info'));
    }
    //月车主接单
    public function get_order_owner_count_month()
    {
        $data = $this->request->param();

        $data['status'] = 1;
        $data['owner_status'] = 1;
        $info = $this->get_month_order_count($data);
        return $this->renderSuccess('', compact('info'));
    }
    //月车主退单
    public function get_order_owner_return_month()
    {
        $data = $this->request->param();

        $data['status'] = 1;
        $data['owner_status'] = 2;
        $info = $this->get_month_order_count($data);
        return $this->renderSuccess('', compact('info'));
    }

    //月订单统计
    public function get_order_months()
    {
        $data = $this->request->param();
        $model = new CarOrderModel;
        $info = $model->get_order_months($data);


        return $this->renderSuccess('', compact('info'));
    }

    //当日订单
    public function get_days_order($type,$data)
    {

        if($data['date']){
            $times = $this->get_time($data);
            $where[] = ['create_time','between',$times['date']];
        }
        if($type==1){
            $where[] = ['is_delete','=',0];
            $where[] = ['status','=',1];
            $where[] = ['owner_status','<',2];
        }
        if($type==2){//接单
            $where[] = ['is_delete','=',0];
            $where[] = ['status','=',1];
            $where[] = ['owner_status','=',1];
        }
        if($type==3){//退单
            $where[] = ['is_delete','=',0];
            $where[] = ['status','=',1];
            $where[] = ['owner_status','=',2];
        }
        $model = new CarOrderModel;

        $info = $model->field("FROM_UNIXTIME(create_time,'%Y-%m-%d') as gap,COUNT(*) as tnum,sum(price) as amount")
            ->group('gap')
            ->where($where)
            ->select()->toArray();


        return $info;
    }

    public function get_month_order_count($data)
    {
        $model = new CarOrderModel;
        if($data['date']){
            //$times = $model->date_time(3,strtotime($data['date']));
            $now = strtotime($data['date'][0]);

            $stime = strtotime(date('Y-m-d 00:00:00', mktime(0, 0, 0, date('m', $now), '1', date('Y', $now))));
            $etime = strtotime(date('Y-m-d 23:39:59', mktime(0, 0, 0, date('m', $now), date('t', $now), date('Y', $now))));

            $where[] = ['create_time','between',[$stime,$etime]];
        }

        if($data['status']){
            $where[] = ['status','=',$data['status']];
        }
        if(isset($data['owner_status'])){
            $where[] = ['owner_status','=',$data['owner_status']];
        }
        $info = $model->field("FROM_UNIXTIME(create_time,'%Y-%m') as gap,COUNT(*) as tnum,sum(price) as amount")
            ->group('gap')
            ->where($where)
            ->select()->toArray();

        return $info;
    }

    /*
     * 销售统计导出
     * */
    public function sale_push()
    {

        $data = $this->request->param();
        if($data['type']==1){
            $list = $this->get_days_order(1,$data);
        }else{
            $data['status'] = 1;
            $list = $this->get_month_order_count($data);
        }


        $tableHeader = array('日期','订单数', '销售额');
        $path = $this->excel($list,$tableHeader,'销售统计');

        return $this->renderSuccess('导出成功！',['path'=>'http://'.$_SERVER['HTTP_HOST'].$path]);

    }
    /*
     * 订单统计导出
     * */
    public function order_push()
    {
        $type = $this->request->param('type');
        $date = $this->request->param();
        $model = new CarOrderModel;
        if($type==1){
            $list = $model->get_order_day_count($date);
        }else{
            $list = $model->get_order_months($date);
        }
        //dump($list);die;
        $tableHeader = array('日期','已完成订单数', '车主取消订单数', '已接单数', '未接单数', '用户取消订单数', '已支付订单数', '未支付订单数');

        $path = $this->excel($list,$tableHeader,'订单统计');

        return $this->renderSuccess('导出成功！',['path'=>'http://'.$_SERVER['HTTP_HOST'].$path]);
    }

    /*
     * 车主退单统计导出
     * */
    public function owner_return_push()
    {
        $data = $this->request->param();
        if($data['type']==1){
            $list = $this->get_days_order(3,$data);
        }else{
            $data['status'] = 1;
            $data['owner_status'] = 2;
            $list = $this->get_month_order_count($data);
        }

        $tableHeader = array('日期','订单数', '退单金额');
        $path = $this->excel($list,$tableHeader,'车主退单统计');

        return $this->renderSuccess('导出成功！',['path'=>'http://'.$_SERVER['HTTP_HOST'].$path]);
    }
    /*
     * 车主接单统计导出
     * */
    public function owner_order_push()
    {
        $data = $this->request->param();
        if($data['type']==1){
            $list = $this->get_days_order(2,$data);
        }else{
            $data['status'] = 1;
            $data['owner_status'] = 1;
            $list = $this->get_month_order_count($data);
        }

        $tableHeader = array('日期','订单数', '接单金额');
        $path = $this->excel($list,$tableHeader,'车主接单统计');

        return $this->renderSuccess('导出成功！',['path'=>'http://'.$_SERVER['HTTP_HOST'].$path]);
    }


    /**
     * 导出Excel表格
     * @param $data 打印的数据  数组形式
     * @param $tableHeader 表头 数组形式
     * @param $fileName 保存的文件名 字符串格式
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Writer_Exception
     */
    public function excel($data,$tableHeader,$fileName)
    {
        include_once (root_path().'./extend/PHPExcel/PHPExcel.php');
        //创建对象
        $excel =  new \PHPExcel();
        //Excel表格式
        $letter = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S',
            'T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL',
            'AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
        //填充表头信息/1是格式
        $tableHeader=$tableHeader;
        for ($i = 0; $i < count($tableHeader); $i++)
        {
            $excel->getActiveSheet()->setCellValue("$letter[$i]1", "$tableHeader[$i]");
        }

        //填充表格信息
        for ($i = 2; $i <= count($data) + 1; $i++)
        {
            $j = 0;
            foreach ($data[$i - 2] as $key => $value)
            {
                $excel->getActiveSheet()->setCellValue("$letter[$j]$i", "$value");
                $j++;
            }
        }

        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel5($excel);
        /*header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
        header("Content-Transfer-Encoding:binary");*/
        $path = '/uploads/xls/'.$fileName.'.xls';
        $write->save(root_path().'/public'.$path);
        return $path;
    }

    public function get_time($data)
    {
        if($data['date'][0] == $data['date'][1]){
            $time['date'][0] = strtotime($data['date'][0].' 00:00:00');
            $time['date'][1] = strtotime($data['date'][1].' 23:59:59');
        }else{
            $time['date'][0] = strtotime($data['date'][0].' 00:00:00');
            $time['date'][1] = strtotime($data['date'][1].' 23:59:59');
        }
        return $time;
    }



}